package pl.lodz.p.mbus;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

import android.util.Xml;

public class RoomXmlParser {
	
	private static final String ns = null;
	   
	    public List<Sensor> parse(InputStream in) throws XmlPullParserException, IOException {
	        try {
	            XmlPullParser parser = Xml.newPullParser();
	            parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false);
	            parser.setInput(in, null);
	            parser.nextTag();
	            return readFeed(parser);
	        } finally {
	            in.close();
	        }
	    }
	    
	    private List<Sensor> readFeed(XmlPullParser parser) throws XmlPullParserException, IOException {
	        List <Sensor>entries = new ArrayList<Sensor>();

	        parser.require(XmlPullParser.START_TAG, ns, "SensorList");
	        while (parser.next() != XmlPullParser.END_TAG) {
	            if (parser.getEventType() != XmlPullParser.START_TAG) {
	                continue;
	            }
	            String name = parser.getName();
	            // Starts by looking for the entry tag
	            if (name.equals("Sensor")) {
	            	Sensor s = readEntry(parser);
	                entries.add(s);
	            } else {
	                skip(parser);
	            }
	        }  
	        return entries;
	    }
	    
	    private Sensor readEntry(XmlPullParser parser) throws XmlPullParserException, IOException {
	        parser.require(XmlPullParser.START_TAG, ns, "Sensor");
	        
	        String name="";
		    String icon="";
		    String set = "";
		    String current="";
		    String max="";
	        
	        
	        while (parser.next() != XmlPullParser.END_TAG) {
	            if (parser.getEventType() != XmlPullParser.START_TAG) {
	                continue;
	            }
	            String tagName = parser.getName();
	            if (tagName.equals("Name")) 
	            	name = readValue(parser, tagName);
	            else if (tagName.equals("Icon")) 
	            	icon = readValue(parser, tagName);
	            else if (tagName.equals("SetTemperatureRegister")) 
	            	set = readValue(parser, tagName);
	            else if (tagName.equals("CurrentTemperatureRegister")) 
	            	current = readValue(parser, tagName);
	            else if (tagName.equals("MaxTemperatureRegister")) 
	            	max = readValue(parser, tagName);
	            else
	            	skip(parser);
	        }
	        
	        return new Sensor (name, icon, set, current, max);
	     
	    }
	    
	    private String readValue(XmlPullParser parser,String tagName) throws IOException, XmlPullParserException {
	        String value = "";
	    	parser.require(XmlPullParser.START_TAG, ns, tagName);
	        value = readText(parser);
	        parser.require(XmlPullParser.END_TAG, ns, tagName);
	        return value;
	    }
	    
	    private String readText(XmlPullParser parser) throws IOException, XmlPullParserException {
	        String result = "";
	        if (parser.next() == XmlPullParser.TEXT) {
	            result = parser.getText();
	            parser.nextTag();
	        }
	        return result;
	    }
	    
	    private void skip(XmlPullParser parser) throws XmlPullParserException, IOException {
	        if (parser.getEventType() != XmlPullParser.START_TAG) {
	            throw new IllegalStateException();
	        }
	        int depth = 1;
	        while (depth != 0) {
	            switch (parser.next()) {
	            case XmlPullParser.END_TAG:
	                depth--;
	                break;
	            case XmlPullParser.START_TAG:
	                depth++;
	                break;
	            }
	        }
	     }


}
